package com.huashi.dealer.modular.controller.api.message;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huashi.dealer.commonEntity.Message;
import com.huashi.dealer.commonEntity.MessageCategory;
import com.huashi.dealer.core.page.PageFactory;
import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.message.service.MessageService;
import com.huashi.dealer.modular.business.message.param.MessageResponseParam;
import com.huashi.dealer.modular.business.message.vo.IndexPageVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * API消息控制器
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Tag(name = "API消息控制器")
@RestController("apiMessageController")
@RequestMapping("/api/message")
@Validated
public class MessageController {

    @Resource
    private MessageService messageService;
    /**
     * 获取首页
     */
    @GetMapping("/getPageIndex")
    @Operation(summary = "获取首页")
    public CommonResult<IndexPageVo> getPageIndex() {
        return CommonResult.data( messageService.getPageIndex());
    }
    /**
     * 消息首页
     */
    @GetMapping("/index/{type}")
    @Operation(summary = "消息首页")
    public CommonResult<List<MessageCategory>> getMessageIndex(@PathVariable String type) {
        try {
            List<MessageCategory> responseParam = messageService.getMessageIndex(type);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取消息首页失败：" + e.getMessage());
        }
    }

    /**
     * 消息列表
     */
    @GetMapping("/lists")
    @Operation(summary = "消息列表")
    public CommonResult<Page<MessageResponseParam>> getMessageList(Message message) {
        Page<Message> page = PageFactory.defaultPage();
        try {
            Page<MessageResponseParam> responseParam = messageService.getMessageList(message,page);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取消息列表失败：" + e.getMessage());
        }
    }

    /**
     * 消息详情
     */
    @GetMapping("/detail/{messageId}")
    @Operation(summary = "消息详情")
    public CommonResult<MessageResponseParam> getMessageDetail(@PathVariable Long messageId) {
        try {
            MessageResponseParam responseParam = messageService.getMessageDetail(messageId);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取消息详情失败：" + e.getMessage());
        }
    }

    /**
     * 编辑消息状态
     */
    @GetMapping("/editStatus/{messageId}")
    @Operation(summary = "编辑消息状态")
    public CommonResult<String> editMessageStatus(@PathVariable Long messageId) {
        try {
            messageService.editMessageStatus(messageId);
            return CommonResult.ok("编辑成功");
        } catch (Exception e) {
            return CommonResult.error("编辑消息状态失败：" + e.getMessage());
        }
    }

    /**
     * 清理消息状态
     */
    @GetMapping("/cleanUpStatus")
    @Operation(summary = "清理消息状态")
    public CommonResult<String> cleanUpMessageStatus() {
        try {
            messageService.cleanUpMessageStatus();
            return CommonResult.ok("清理成功");
        } catch (Exception e) {
            return CommonResult.error("清理消息状态失败：" + e.getMessage());
        }
    }
} 